Quick Index
What and Why


A queue structure behaves like a line of vehicles at a traffic signal.

The first vehicle added to the queue is also the first out.

Another example of a queue is a line of citizens lined up to vote.

This is called FIFO (first in first out). Contrast this to LIFO (stack).
🚦🚗🚚🚙🚗
Queue has a broad range of uses.

A few examples:

  • Job Scheduling (Prioritized)
  • CPU Scheduling
  • Traffic Simulation
  • Register Simulation
  • Any real-World Queue Simulation


Fundamental Operations


A queue has two fundamental operations which are "enqueue" and "dequeue".

The method "enqueue" adds (appends) an element to the rear of the queue.

The element is added to the "rear" of the queue (the "rear" is also called "last", "tail", or "end").
The method "dequeue" removes (and returns) the element from the front of the queue.

The element is removed from the "front" of the queue (the "front" is also called "first", "head", or "start").


FIFO


FIFO - First in first out

Enqueue #1 (Add)
Enqueue (add) a "Green Honda" to empty list.
Enqueue #2 (Add)
Enqueue (add) a "Blue Ford". It is added to end (last).
Enqueue (Add) Two More
Add two more elements.

  • Enqueue "White Toyota"
  • Enqueue "Silver Hyundai"

The elements are added to the end of the list (in order they are added).
Dequeue #1 (Remove)
We do a dequeue (remove) operation and the first element in the list is removed (and returned).
Dequeue #2 (Remove)
Now the "Blue Ford" is first.

We do a dequeue (remove) operation and again the first element in the list is removed (and returned).
Dequeue #3 (Remove)
Now the "White Toyota" is first.

We do a dequeue (remove) operation and again the first element in the list is removed (and returned).

Traversal (Iteration)


The following shows the default traversal order. The order follows what would happen on successive dequeues.

Note that if we traverse (iterate) through the structure we are generally not removing elements.

Any of these terms might be used: traverse, iterate, navigate, loop.

Example
The default traversal for a queue is from first to last (or front to rear).

We would visit the example in this order:

Honda, Ford, Toyota, Hyundai



ADT


See the ADT here...

References




Navigation